package com.init.iblog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.init.iblog.constants.SystemConstants;
import com.init.iblog.domain.ResponseResult;
import com.init.iblog.domain.dto.LinkDto;
import com.init.iblog.domain.entity.Link;
import com.init.iblog.domain.vo.LinkVo;
import com.init.iblog.domain.vo.PageVo;
import com.init.iblog.enums.AppHttpCodeEnum;
import com.init.iblog.exception.SystemException;
import com.init.iblog.mapper.LinkMapper;
import com.init.iblog.service.LinkService;
import com.init.iblog.utils.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * (Link)表服务实现类
 *
 * @author makejava
 * @since 2023-02-19 15:24:39
 */
@Service("linkService")
public class LinkServiceImpl extends ServiceImpl<LinkMapper, Link> implements LinkService {

    @Autowired(required = false)
    private LinkMapper linkMapper;

    /**
     * 查询所有友链
     * @return
     */
    @Override
    public ResponseResult getAllLink() {
        //查询所有审核通过的友链
        LambdaQueryWrapper<Link> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Link::getStatus, SystemConstants.LINK_AUDIT_STATUS_NORMAL);
        List<Link> links = list(lambdaQueryWrapper);
        //转换成Vo
        List<LinkVo> linkVos = BeanCopyUtils.copyBeanList(links, LinkVo.class);
        return ResponseResult.okResult(linkVos);
    }

    @Override
    public ResponseResult<PageVo> pageLinkList(Integer pageNum, Integer pageSize, LinkDto linkDto) {

        LambdaQueryWrapper<Link> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.hasText(linkDto.getName()),Link::getName,linkDto.getName());
        queryWrapper.eq(StringUtils.hasText(linkDto.getStatus()),Link::getStatus,linkDto.getStatus());
        Page<Link> page = new Page<>();
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        page(page,queryWrapper);
        PageVo pageVo = new PageVo(page.getRecords(),page.getTotal());
        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult getLink(Long id) {


        Link link = linkMapper.selectById(id);


        return ResponseResult.okResult(link);
    }

    @Override
    public ResponseResult addLink(Link link) {

        if(!StringUtils.hasText(link.getName())){
            throw new SystemException(AppHttpCodeEnum.CONTENT_NOT_NULL);
        }

        if(!StringUtils.hasText(link.getAddress())){
            throw new SystemException(AppHttpCodeEnum.CONTENT_NOT_NULL);
        }
        save(link);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult updateLink(Link link) {

        linkMapper.updateById(link);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult deleteLink(List<Long> ids) {
        LambdaQueryWrapper<Link> queryWrapper = new LambdaQueryWrapper<>();
        for(Long id : ids){
            linkMapper.deleteById(id);
        }
        return ResponseResult.okResult();
    }
}
